package com.yupi.yuoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.yuoj.common.ErrorCode;
import com.yupi.yuoj.exception.BusinessException;
import com.yupi.yuoj.model.entity.Content;
import com.yupi.yuoj.model.entity.ContentLike;
import com.yupi.yuoj.model.entity.User;
import com.yupi.yuoj.service.ContentLikeService;
import com.yupi.yuoj.service.ContentService;
import com.yupi.yuoj.mapper.ContentLikeMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author admin
* @description 针对表【content_like(内容点赞表)】的数据库操作Service实现
* @createDate 2025-11-01 21:28:49
*/
@Service
public class ContentLikeServiceImpl extends ServiceImpl<ContentLikeMapper, ContentLike>
    implements ContentLikeService{

    @Resource
    private ContentService contentService;

    /**
     * 内容点赞
     *
     * @param contentId
     * @param loginUser
     * @return
     */
    @Override
    public int doContentLike(long contentId, User loginUser) {
        // 判断实体是否存在，根据类别获取实体
        Content content = contentService.getById(contentId);
        if (content == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        long userId = loginUser.getId();
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        ContentLikeService contentLikeService = (ContentLikeService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return contentLikeService.doContentLikeInner(userId, contentId);
        }
    }

    /**
     * 封装了事务的方法
     *
     * @param userId
     * @param contentId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int doContentLikeInner(long userId, long contentId) {
        ContentLike contentLike = new ContentLike();
        contentLike.setUser_id(userId);
        contentLike.setContent_id(contentId);
        QueryWrapper<ContentLike> contentLikeQueryWrapper = new QueryWrapper<>(contentLike);
        ContentLike oldContentLike = this.getOne(contentLikeQueryWrapper);
        boolean result;
        // 已点赞
        if (oldContentLike != null) {
            result = this.remove(contentLikeQueryWrapper);
            if (result) {
                // 点赞数 - 1
                Content content = contentService.getById(contentId);
                Integer likeCount = content.getLike_count();
                if (likeCount != null && likeCount > 0) {
                    Content updateContent = new Content();
                    updateContent.setId(contentId);
                    updateContent.setLike_count(likeCount - 1);
                    contentService.updateById(updateContent);
                }
                return -1;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(contentLike);
            if (result) {
                // 点赞数 + 1
                Content content = contentService.getById(contentId);
                Integer likeCount = content.getLike_count();
                if (likeCount == null) {
                    likeCount = 0;
                }
                Content updateContent = new Content();
                updateContent.setId(contentId);
                updateContent.setLike_count(likeCount + 1);
                contentService.updateById(updateContent);
                return 1;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }

}




